package com.atguigu.tingshu.payment.config;

import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "wechat.v3pay") //读取节点
@Data
public class WxPayV3Config {

    /**
     * 商户号
     */
    public String merchantId;
    /**
     * 商户API私钥路径
     */
    public String privateKeyPath;
    /**
     * 商户证书序列号
     */
    public String merchantSerialNumber;
    /**
     * 商户APIV3密钥
     */
    public String apiV3key;
    private String appid;
    /**
     * 回调地址
     */
    private String notifyUrl;

    @Bean
    public RSAAutoCertificateConfig rsaAutoCertificateConfig() {
        return new RSAAutoCertificateConfig.Builder()
                .merchantId(this.merchantId)
                .privateKeyFromPath(privateKeyPath)
                .merchantSerialNumber(merchantSerialNumber)
                .apiV3Key(apiV3key)
                .build();
    }

    /**
     * 调用JSAPI-小程序、APP场景使用的服务类对象
     *
     * @return
     */
    @Bean
    public JsapiServiceExtension service() {
        return new JsapiServiceExtension.Builder().config(rsaAutoCertificateConfig()).build();
    }
}
